<template>
  <!-- 编辑页面 -->
  <el-dialog :title="title[state]" :visible.sync="visible" :close-on-click-modal="false" append-to-body width="800px" @close="handleClose">
    <el-form ref="dataForm" :rules="rules" :model="formData" label-position="right" label-width="100px">
      <el-input v-model="formData.id" type="hidden" />
      <el-input v-model="formData.dictType" type="hidden" />
      <el-row>
        <el-col :span="12">
          <el-form-item label="枚举键值" prop="code">
            <el-input v-model="formData.code" :readonly="state==='update'" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="枚举描述" prop="text">
            <el-input v-model="formData.text" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="使用状态" prop="status">
            <el-select v-model="formData.status" class="filter-item" style="width: 100%;" placeholder="请选择">
              <el-option v-for="item in statusOptions" :key="item.code" :label="item.text" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="handleClose">
        取消
      </el-button>
      <el-button :loading="submitting" type="primary" icon="el-icon-check" @click="handleSubmitFormData">
        <span v-if="!submitting">确认</span>
        <span v-else>提 交 中...</span>
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { handleSave } from '@/views/sys/dict/dict-code'
import waves from '@/directive/waves' // waves

export default {
  // 当前页面名称（组件）
  name: 'DictDataEdit',
  // 引用的组件
  components: {},
  // 指令
  directives: { waves },
  filters: {},
  // props属性
  props: {
    dictEditParams: {
      type: Object,
      require: true,
      default: undefined
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 状态字典
      statusOptions: [{ code: 'Y', text: '启用' }, { code: 'N', text: '禁用' }],
      // 是否显示弹出层
      visible: false,
      // 表单数据对象
      formData: {},
      // 弹出层状态
      state: '',
      // 弹窗层标题
      title: {
        update: '修改',
        create: '新增'
      },
      submitting: false,
      // 表单校验规则
      rules: {
        text: [{ required: true, message: '枚举描述不能为空', trigger: 'change' }],
        code: [{ required: true, message: '枚举键值不能为空', trigger: 'change' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    init() {
      this.visible = this.dictEditParams.visible
      this.state = this.dictEditParams.state
      this.formData = this.dictEditParams.formData
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 重置表单数据
    handleRestFormData() {
      this.formData = {
        id: undefined,
        code: undefined,
        text: undefined,
        dictType: this.dictEditParams.formData.dictType,
        status: 'Y',
        remark: ''
      }
    },
    // 提交表单数据
    handleSubmitFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submitting = true
          handleSave(this.formData).then((res) => {
            this.submitting = false
            // 这里要刷新父页面的列表
            this.$parent.$parent.handleFilterQuery()
            this.visible = false
            this.$message({
              showClose: true,
              message: res.msg,
              type: 'success',
              duration: 4000
            })
          })
        }
      })
    },
    // 弹出层关闭回调
    handleClose() {
      this.handleRestFormData()
      // 这里要刷新父页面的列表，由于有个遮罩层，需要两个$parent
      this.$parent.$parent.handleFilterQuery()
      this.visible = false
    }
  }
}
</script>
